package com.abel.bigwater.dataserver.model

import com.abel.bigwater.model.JsonHelper
import com.alibaba.fastjson.annotation.JSONField
import org.springframework.data.mongodb.core.index.Indexed
import org.springframework.data.mongodb.core.mapping.Document
import java.util.*

@Document(collection = "config")
data class ServiceConfig(
        @Indexed(unique = true)
        var cfgId: String? = null,

        /**
         * 水司标示，昆明通用: 37, 昆明清源: 41.
         */
        var firmId: String? = null,

        var firmCode: String? = null,

        var aesPassword: String? = null,

        var localPort: Int? = null,
        var remoteHost: String? = null,
        var remotePort: Int? = null,

        /**
         * 当前状态
         */
        var running: Boolean? = null,

        /**
         * 最后状态
         */
        var lastStatus: String? = null,

        var sleepSeconds: Int = 300,

        /**
         * 启动指令
         */
        var shouldLaunch: Boolean? = null,

        var sendSecret: Boolean? = null,
        var splitPacket: Boolean? = null,
        var uploadSecret: Boolean? = null,

        /** used by kming-client*/
        var fromCollection: String? = null,

        /** used by iwg-server & kming-server.*/
        var toCollection: String? = null) {
    /**
     * display password with stars.
     */
    private var _aesPassShow: String? = null
    var aesPassShow: String?
        get() {
            _aesPassShow = "${aesPassword?.take(3)}***${aesPassword?.takeLast(2)}"
            return _aesPassShow
        }
        set(value) {
            _aesPassShow = value
        }

    /**
     * 最后时间
     */
    @JSONField(format = JsonHelper.FULL_DATE_FORMAT)
    var lastTime: Date? = null

    override fun equals(other: Any?): Boolean {
        if (other !is ServiceConfig) return false

        return other.cfgId == this.cfgId && other.localPort == this.localPort
                && other.remoteHost == this.remoteHost
                && other.remotePort == this.remotePort
                && other.fromCollection == this.fromCollection
                && other.toCollection == this.toCollection
    }
}